package com.atguigu.gmall.realtime.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Felix
 * @date 2022/12/13
 * 分词工具类
 */
public class KeywordUtil {
    //分词
    public static List<String> analyze(String text) {
        List<String> keywordList = new ArrayList<>();

        StringReader reader = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(reader, true);
        Lexeme lexeme = null;

        try {
            while ((lexeme = ik.next()) != null) {
                String keyword = lexeme.getLexemeText();
                keywordList.add(keyword);
            }
        }catch(IOException e){
            e.printStackTrace();
        }

        return keywordList;
    }

    public static void main(String[] args) {
        List<String> keywordList = analyze("Apple iPhoneXSMax (A2104) 256GB 深空灰色 移动联通电信4G手机 双卡双待");
        System.out.println(keywordList);
    }
}
